<style>
body{
	background-color: #CCCCCC;
	font-family: Verdana;
	fotn-family: Verdana;
}
.frame{
	background-color: #FFFFFF;
	border-style: solid;
	border-width: 1 1 1 1;
	border-color: #000000;
}
.frame th{
	background-color: #003C78;
	color: #FFFFFF;
        border-style: solid;
        border-width: 0 0 1 1;
        border-color: #000000;
}
.inner{
        border-style: solid;
        border-width: 1 1 0 0;
        border-color: #CCCCCC;
}
.inner th{
        background-color: #CCCCCC;
        color: #000000;
        border-style: solid;
        border-width: 1 1 1 1;
        border-color: #CCCCCC;
}
.inner td{
        color: #000000;
        border-style: solid;
        border-width: 0 0 1 1;
        border-color: #CCCCCC;
	padding: 2 5 2 15;
}
.menu{
	padding:  1 5 5 5;
	border-style: solid;
	border-width: 1 1 0 0 ;
	border-color: #CCCCCC;	
	
}
.menu td{
	padding:  1 5 5 5;
	border-style: solid;
	border-width: 0 0 1 1;
	border-color: #CCCCCC;	
}

.menu a{
	color: #003C78;
}

input, textarea, select {
	font-family: lucidatypewriter, courier, fixed; font-size: 9pt; padding: 2;
	border-width: 1px; 
	border-color: #CCCCCC;
	border-style: solid;
	background-color: #FFFFFF;
	
}

</style>
<?
//--------------------------------------------------------
include ('../../variables.php');

$dbh = get_master_connect();
if (!$dbh) {
        echo "We are sorry but we cannot process your request at the moment<BR>";
        exit_now();
}
//--------------------------------------------------------


$id = $_POST[id];
$union_date[] = $_POST[union_date];
$prl = $_POST[prl];
//--------------------------------------------------------

function get_sales($id,$date=''){
        global $session_db_name,$dbh;	
	if($date) $where = " AND date > '$date' ";

	$SQL = "select sum(price-real_price) as sum,date_format(date,'%Y-%m') as date  from sales where id='$id' and refund_type is null $where GROUP by date_format(date,'%Y-%m');";

	if (!($result = @mysql_db_query($session_db_name, $SQL, $dbh))) exit("ERR - 1");
	while ($row = @mysql_fetch_assoc($result)) {
		$sales[$row[date]] = $row[sum];
	}
	return $sales;
}
//--------------------------------------------------------
function get_affiliates($id){
        global $session_db_name,$dbh;
	$SQL = "select
	date_format(date,'%Y-%m') as date,
	sum(real_price)*0.1 as sum
	FROM reseller_links left join sales on (child_id=id and link_type='affiliate')
	where
	type='plan' and
	state = 'normal' and
	parent_id='$id'
	group by date_format(date,'%Y-%m');";

	if (!($result = @mysql_db_query($session_db_name, $SQL, $dbh))) exit("ERR - 2");
	while ($row = @mysql_fetch_assoc($result)) {
		$affiliates[$row[date]] = $row[sum];
	}
	return $affiliates;
}
//--------------------------------------------------------
function get_unions($id){
        global $session_db_name,$dbh;
	$SQL = "select * from  reseller_links where parent_id=$id and link_type='union' and child_id!=$id;";
	if ( !($result = @mysql_db_query($session_db_name, $SQL, $dbh))) exit("ERR 3");
	while ($row = @mysql_fetch_assoc($result)) {
		$unions[] = $row;
	}
	return $unions;
}
//--------------------------------------------------------
function get_reseller_info($id){
        global $session_db_name,$dbh;
        $SQL = "select * from  resellers where id=$id;";
        if ( !($result = @mysql_db_query($session_db_name, $SQL, $dbh))) exit("ERR 4");
        return @mysql_fetch_assoc($result);
}

//--------------------------------------------------------
function get_periods($id){
	global $session_db_name,$dbh;
	$SQL = "select date_format(date,'%Y-%m') as sdate,PERIOD_DIFF(date_format(now(),'%Y%m'), date_format(date,'%Y%m')) as periods from resellers where id='$id' order by date desc";
	if ( !($result = @mysql_db_query($session_db_name, $SQL, $dbh))) exit("ERR 4");
	$row = @mysql_fetch_array($result);
	$signup_date = $row[sdate];
	$periods_count = $row[periods];
	for($i=0;$i<=$periods_count;$i++){
		$bdate = explode("-",$row[sdate]);
		$bdate = date("Y-m",mktime(0,0,0,$bdate[1]+$i,1,$bdate[0]));
		$hash[$bdate]="";
	}
	return $hash;
}
//--------------------------------------------------------


?>
<center>
<form action='' method='POST'>
<table class='frame' cellspacing=0>
	<tr>
		<th align='left'>
			Reseller ID: <input type='text' name='id' size='6' value='<?=$id;?>'>
			<input type='submit' value='Just do it'>
		</th>
	</tr>
<? if($id){ 
	$reseller = get_reseller_info($id);
	echo "
		<tr>
			<td>
			<table width='100%'>
					<tr><td>Reseller : ".$reseller[reseller]."</td></tr>
					<tr><td>Name : ".$reseller[firstname]." ".$reseller[lastname]."</td></tr>
					<tr><td>Store Name: ".$reseller[store_name]."</td></tr>
					<tr><td>Email : ".$reseller[email]."</td></tr>
					<tr><td>Country : ".$reseller[country]."</td></tr>
			</table>
			<hr>
			</td>
		</tr>
	";
	$unis = get_unions($id);
	if(is_array($unis)){
		foreach($unis as $u){
		 echo "<tr>";
			echo "<td colspan='1'> Start UNION with ".$u[child_id]." from :<input type='text' name='union_date[".$u[child_id]."]' value='".$union_date[$u[child_id]]."' size='12'> (Format: YYYY-MM-DD)<td>";
		echo "</tr>";
		}
	}
?>	
	<tr>
		<td colspan='1'>
			<table class='inner' cellspacing=0 width='100%'>
				<tr>
					<th>Date</th>
					<th>Sales</th>
					<th>Affil.</th>
					<? if(count($unis) > 0) {?><th colspan='<?=count($unis)?>'>Unions</th><?}?>
					<th>Total</th>
					<th>Rel.Lev.</th>
					<th>Payments</th>
				</tr>
			<?
				$periods = get_periods($id);
				$sales = get_sales($id);
				$affiliates = get_affiliates($id);								
                                if(is_array($unis)) foreach($unis as $u){
                                	$usales[] = get_sales($u[child_id],$union_date[$u[child_id]]);
                                }
				foreach($periods as $p=>$v){
					echo "<tr onMouseOver=\"this.style.backgroundColor='#CCCCCC';\" onMouseOut=\"this.style.backgroundColor='';\">";
					echo "<td>".$p."</td>";
					echo "<td align='right'>\$ ".(($sales[$p])?($sales[$p]):("0.00"))."</td>";
					$total[$p]+= $sales[$p];
					echo "<td align='right'>\$ ".(($affiliates[$p])?($affiliates[$p]):("0.00"))."</td>";
					$total[$p]+= $affiliates[$p];
					if(is_array($usales)) foreach($usales as $us){
						 echo "<td align='right'>\$ ".(($us[$p])?($us[$p]):("0.00"))."</td>";
						 $total[$p]+= $us[$p];
					}
					echo "<td align='right'>\$ ".(($total[$p])?(number_format($total[$p], 2, '.', ' ')):("0.00"))."</td>";
					if(!$prl[$p]) $prl[$p] = (int) $reseller[current_release_level];
					echo "<td> <select name='prl[".$p."]'>";
					$opts = "
					  <option value='50'>$50</option>
					  <option value='100'>$100</option>
					  <option value='200'>$200</option>
					  <option value='300'>$300</option>
					  <option value='400'>$400</option>
					  <option value='500' >$500</option>
					  <option value='1000'>$1000</option>
					  <option value='1500'>$1500</option>
					  <option value='2000'>$2000</option>
					  <option value='2500'>$2500</option>
					  <option value='3000'>$3000</option>
					  <option value='5000'>$5000</option>
					";
					echo str_replace("'".$prl[$p]."'","'".$prl[$p]."' selected",$opts);
					echo "</select></td>";
					if($total_payed >= $prl[$p] && $periods_count >= 2){
	                                        echo "<td align='right'>\$ ".(($total_payed)?(number_format($total_payed, 2, '.', ' ')):("-"))."</td>";
						$mega_total += $total_payed;
						$total_payed = 0;
						$periods_count = 1;
					}
					else{
						$total_payed += $total[$p];
						$periods_count++;
						echo "<td align='right'>-</td>";
					}
					echo "</tr>";
				}
			?>
			<tr>
				<td><b><?=count($periods);?></b></td>
				<td align='right'><b>$ <?=(is_array($sales)?array_sum($sales):'0.00');?></b></td>
				<td align='right'><b>$ <?=(is_array($affiliates)?array_sum($affiliates):'0.00');?></b></td>
				<?
                                        if(is_array($usales)) foreach($usales as $us){
						if(!is_array($us)) $us = array();
                                                 echo "<td align='right'><b>\$ ".array_sum($us)."</b></td>";
                                        }				
				?>
				<td align='right'><b>$ <?=(is_array($total)?array_sum($total):'0.00');?></b></td>
				<td align='right'>&nbsp;</td>
				<td align='right'><b>$ <?=($mega_total?$mega_total:'0.00');?></b></td>
			</tr>
			</table>
		</td>
	</tr>
<? } ?>
</table>
</form>
</center>